pub(crate) mod resp;

use serde::Deserialize;
use tokio::fs::File;
use tokio::io::AsyncReadExt;

#[derive(Deserialize, Debug)]
pub struct WebCfg {
    pub host: String,
    pub port: String,
}

#[derive(Deserialize, Debug)]
pub struct Configuration {
    pub web: WebCfg,
}

impl Configuration {
    pub fn get_bind_listener(&self) -> String {
        return format!("{}:{}", self.web.host, self.web.port);
    }
    pub async fn from_env() -> Result<Self, anyhow::Error> {
        // let env = std::env::var("ENV").unwrap_or_else(|_| "development".into());
        // match env.as_ref() {
        //     "production" => config_loader!("prod.toml"),
        //     "development" => config_loader!("config.toml"),
        //     _ => config_loader!("config.toml"), // 默认加载开发配置
        // }
        // TODO

        match File::open("./config/app.toml").await {
            Ok(mut file) => {
                let mut buf = String::new();

                match file.read_to_string(&mut buf).await {
                    Ok(_) => {}
                    Err(e) => return Err(anyhow::Error::new(e)),
                }

                match toml::from_str(&buf) {
                    Ok(config) => return Ok(config),
                    Err(e) => return Err(anyhow::Error::new(e)),
                }
            }
            Err(e) => return Err(anyhow::Error::new(e)),
        };
    }
}
